import { ref } from 'vue'
import { defineStore } from 'pinia'
import api from '@/api/home'

import type { BannerItem, LessonListItem } from '@/config/types'

export const useHomeStore = defineStore('homeStore', () => {
  const loading = ref<boolean>(false)
  const banners = ref<BannerItem[]>([])
  const lessons = ref<LessonListItem[]>([])
  const notice = ref<string>('')

  async function loadHome() {
    loading.value = true
    try {
      const res = await api.get()
      banners.value = res.data.banners
      lessons.value = res.data.lessons
      notice.value = res.data.notice
    } finally {
      loading.value = false
    }
  }

  return {
    loading,
    banners,
    lessons,
    notice,

    loadHome
  }
})
